package de.dvdverleih.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.dvdverleih.dao.DaoException;
import de.dvdverleih.dao.DvdDao;
import de.dvdverleih.model.Dvd;

/**
 * @author Landsberger, Neitzsch, Baier
 */

@Controller
public class DvdController {
	private static Logger log = Logger.getLogger(DvdController.class);
	private DvdDao dvdDao = null;

	public void setDvdDao(DvdDao dvdDao) {
		this.dvdDao = dvdDao;
	}

	/**
	 * 
	 * <p>Creates a new {@link Dvd}.</p>
	 * <p>Expected HTTP Get and request '/DvdAnlegen.html'.</p>
	 * @param id primary key of the dvd
	 * 
	 */
	@RequestMapping(value = "/DvdAnlegen.html", method = RequestMethod.GET)
	public ModelAndView edit1(@RequestParam(required = false) Integer id) {
		log.debug("DvdAnlegen.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Dvd());
		} else {
			mv.addObject(dvdDao.findById(id));
		}
		mv.setViewName("dvd-anlegen");
		return mv;
	}
	
	/**
	 * 
	 * <p>Edits a {@link Dvd}.</p>
	 * <p>Expected HTTP Get and request '/DvdBearbeiten.html'.</p>
	 * @param id primary key of the dvd
	 *  
	 */
	@RequestMapping(value = "/DvdBearbeiten.html", method = RequestMethod.GET)
	public ModelAndView edit2(@RequestParam(required = false) Integer id) {
		log.debug("DvdBearbeiten.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Dvd());
		} else {
			mv.addObject(dvdDao.findById(id));
		}
		mv.setViewName("dvd-bearbeiten");
		return mv;
	}

	/**
	 * 
	 * <p>Saves a {@link Dvd} in the database.</p>
	 * <p>Expected HTTP POST and request '/SpeichereDvd.html'.</p>
	 * @return a redirect to page '/DvdAnlegen.html' for creating new dvds
	 * 
	 */
	@RequestMapping(value = "/SpeichereDvd.html", method = RequestMethod.POST)
	public String save1(Dvd dvd, Model model) {
		dvdDao.save(dvd);
		return "redirect:DvdAnlegen.html";
	}
	
	/**
	 * 
	 * <p>Saves a {@link Dvd} in the database, if changes have been made.</p>
	 * <p>Expected HTTP POST and request '/SpeichereDvdAenderung.html'.</p>
	 * @return a redirect to page '/AlleDvdsAdmin.html' for editing dvds
	 * 
	 */
	@RequestMapping(value = "/SpeichereDvdAenderung.html", method = RequestMethod.POST)
	public String save2(Dvd dvd, Model model) {
		dvdDao.save(dvd);
		return "redirect:AlleDvdsAdmin.html";
	}

	/**
	 * 
	 * <p>Deletes a {@link Dvd} from database.</p>
	 * <p>Expected HTTP GET and request '/LoescheDvd.html'.</p>
	 * @return a redirect to page '/AlleDvdsAdmin.html' for editing dvds
	 * 
	 */
	@RequestMapping(value = "/LoescheDvd.html", method = RequestMethod.GET)
	public ModelAndView delete(Dvd dvd) {
		try{
		dvdDao.delete(dvd);
		return new ModelAndView ("redirect:AlleDvdsAdmin.html");}
		catch(DaoException e){
			ModelAndView mv = new ModelAndView("exception-error");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}

	/**
	 * 
	 * <p>Page of the admin for editing a {@link Dvd}.</p>
	 * <p>Finds all dvds from the database.</p>
	 * <p>Admin can edit or delete {@link Dvd}.</p>
	 * <p>Expected HTTP GET and request '/AlleDvdsAdmin.html'.</p>
	 * 
	 */
	@RequestMapping(value = "/AlleDvdsAdmin.html", method = RequestMethod.GET)
	public ModelAndView findAll1() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Dvd");
		mv.addObject("message", "Alle Dvds");
		mv.addObject("dvd", dvdDao.findAll());
		mv.setViewName("list-dvd-admin");
		return mv;
	}
	
	/**
	 * 
	 * <p>Page of the customer to see all {@link Dvd}s.</p>
	 * <p>Finds all dvds from the database.</p>
	 * <p>Expected HTTP GET and request '/AlleDvdsKunde.html'.</p>
	 * 
	 */
	@RequestMapping(value = "/AlleDvdsKunde.html", method = RequestMethod.GET)
	public ModelAndView findAll2() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Dvd");
		mv.addObject("message", "Alle Dvds");
		mv.addObject("dvd", dvdDao.findAll());
		mv.setViewName("list-dvd-kunde");
		return mv;
	}

	/**
	 * 
	 * <p>Search a {@link Dvd} from the database.</p>
	 * <p>Expected HTTP GET and request '/DvdSuchen.html'.</p>
	 * 
	 */
	@RequestMapping(value = "/DvdSuchen.html", method = RequestMethod.GET)
	public ModelAndView suche() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Dvd");
		mv.addObject("message", "Suche");
		mv.addObject("dvd", dvdDao.findAll());
		mv.setViewName("dvd-suche");
		return mv;
	}

	
}
